import axios from "axios";
import ElementUI from 'element-ui';
// 调用里面的mutations下REMOVE_USERINFO
import store from './store'
//出错会进行跳转
import router from './router'

axios.defaults.baseURL='http://localhost:9999'

//前置拦截
axios.interceptors.request.use(config=>{
    return config;
})
//后置拦截，后端传递过来异常信息会拦截,走完axios拦截才会走跳转页面的逻辑
axios.interceptors.response.use(response=>{
    let resp = response.data;
    console.log(resp)
    if (resp.code===200){
        return response;
    }else {
        ElementUI.Message.error(resp.msg,{duration:1*1000});
        return Promise.reject(resp.msg);
    }
   
},
    
    error => {
        console.log(error);
        //如果error.response.data.msg不为空
        if (error.response.data.msg){
            error.message=error.response.data.msg;
        }
        
        if (error.response.status===401){
            store.commit("REMOVE_USERINFO");
            //跳转vue视图
            router.push('/login');
        }else {
            ElementUI.Message.error(error.message,{duration:1*1000});
            //停止，并在console中报错
            // createError.js?2d83:16 Uncaught (in promise) Error: Request failed with status code 400
            return Promise.reject(error);
        }
        
    }
)